import 'package:backend.core/core/domain/entities/web/web_one_time_purchase_item.dart';
import 'package:equatable/equatable.dart';
import 'package:backend.purchases/infrastructure/dtos/api/api_money_amount_dto.dart';
import 'package:backend.purchases/infrastructure/dtos/api/web/api_web_purchase_item_dto.dart';

const kApiWebOneTimePurchaseItemTypeValue = 'one_time_purchase_item';

class ApiWebOneTimePurchaseItemDto extends Equatable implements ApiWebPurchaseItemDto {
  const ApiWebOneTimePurchaseItemDto._internal({
    required this.sku,
    required this.priceDto,
    required this.title,
    required this.legacy,
  });

  @override
  final String sku;

  @override
  final ApiMoneyAmountDto priceDto;

  @override
  final String title;

  @override
  final bool legacy;

  @override
  String get type => kApiWebOneTimePurchaseItemTypeValue;

  @override
  List<Object?> get props {
    return [
      sku,
      priceDto,
      title,
      legacy,
    ];
  }

  factory ApiWebOneTimePurchaseItemDto.fromEntity(WebOneTimePurchaseItem oneTimePurchaseItem) {
    final priceDto = ApiMoneyAmountDto.fromMoneyAmount(oneTimePurchaseItem.price);

    return ApiWebOneTimePurchaseItemDto._internal(
      sku: oneTimePurchaseItem.sku,
      priceDto: priceDto,
      title: oneTimePurchaseItem.title,
      legacy: oneTimePurchaseItem.legacy,
    );
  }

  @override
  Map<String, dynamic> toJson() {
    return {
      kApiWebPurchaseItemTypeKey: type,
      kApiWebPurchaseItemSkuKey: sku,
      kApiWebPurchaseItemPriceKey: priceDto.toJson(),
      kApiWebPurchaseItemTitleKey: title,
      kApiWebPurchaseItemLegacyKey: legacy,
    };
  }
}
